/* ----------------------------------------------------------------------------
 * Copyright (c) 2008 by Airbus SAS. All rights reserved. All intellectual
 * property rights in and/or in the computer program and its related
 * documentation and technology are the sole Airbus' property. This computer
 * program is under Airbus copyright and cannot be in whole or in part
 * reproduced, sublicensed, leased, sold or used in any form or by any means,
 * including without limitation graphic, electronic, mechanical, photocopying,
 * recording, taping or information storage and retrieval systems without Airbus
 * prior written consent. The downloading, exporting or reexporting of this
 * computer program or any related documentation or technology is subject to any
 * export rules, including US regulations.
 * ----------------------------------------------------------------------------
 */

package com.azzaou.util;
import java.io.Serializable;

import org.apache.commons.lang.StringUtils;

import com.azzaou.enums.StatusException;

/**
 * ErrorHandler
 * 
 * @version : V1.0
 * @author : moabdi
 * @Project : [AWN_PACAT_ENGINEERING_ADMINTOOLS]
 * @Date : juil. 2012
 * @DSL_Requirements :
 * @Product_Requirements :
 */
public class ErrorHandler implements Serializable
{
  /**
   * serialVersionUID
   */
  private static final long serialVersionUID = 3123969847287207137L;

  /**
   * errDetails
   */
  private String errDetails = "";

  /**
   * cause
   */
  private String cause;

  /**
   * contact
   */
  private int errorCode;

  /**
   * Exception
   */
  private Exception exception;
  
  /**
   * errFlag is true if the error are already displayed 
   * 
   */
  private boolean errFlag;

  /**
   * authentication
   */
  private String authenticationError;
  
  /**
   * authentication
   */
  private String authenticationTitle;
  
  /**
   * key
   */
  private String key;

  /**
   * technicalException
   */
  private TechnicalException technicalException;
  
  /**
   * businessException
   */
  private BusinessException businessException;
  
  /**
   * setter ErrDetails
   * 
   * @return String
   */
  public String getErrDetails()
  {
    if (errorCode == StatusException.TECHNICAL_EXCEPTION.getStatusException())
    {
      if(technicalException!=null)
      {
        errDetails = technicalException.getErrDetails();
      }
    }
    return errDetails;
  }

  /**
   * setErrDetails
   * 
   * @param pErrDetails
   *          : pErrDetails
   */
  public void setErrDetails(String pErrDetails)
  {
     this.errDetails = pErrDetails;
  }

  /**getCause
   * 
   * @return the message
   */
  public String getCause()
  {
    if (errorCode == StatusException.BUSINESS_EXCEPTION.getStatusException())
    {
      businessException.setKey(key);
      cause = businessException.getBusinessError();
    }
    else if (errorCode == StatusException.TECHNICAL_EXCEPTION.getStatusException())
    {
      cause = technicalException.getCause();
    }
    return cause;
  }

  /**
   * setCause
   * 
   * @param pCause
   *          the message to set
   */
  public void setCause(String pCause)
  {
    this.cause = pCause;
  }

  /**
   * getErrorCode
   * 
   * @return the errorCode
   */
  public int getErrorCode()
  {
    if(errFlag)
    {
      if(exception != null && !"".equalsIgnoreCase(exception.toString()))
      {
        errorCode = StatusException.TECHNICAL_EXCEPTION.getStatusException();
        technicalException.setException(exception);
      }
      else
      {
        errorCode = StatusException.BUSINESS_EXCEPTION.getStatusException();
      }
    }
    else
    {
      errorCode = 0;
    }
   
    return errorCode;
  }

  /**
   * setErrorCode
   * 
   * @param perrorCode
   *          the errorCode to set
   */
  public void setErrorCode(int perrorCode)
  {
    if(perrorCode!=0)
    {
      errFlag = true;
    }
    this.errorCode = perrorCode;
  }

  /**
   * @return exception
   */
  public Exception getException()
  {
    return exception;
  }

  /**
   * @param exception exception to define
   */
  public void setException(Exception pException)
  {
    if(pException!=null)
    {
      errFlag = true;
    }
    this.exception = pException;
  }

  /**
   * @return errFlag
   */
  public boolean isErrFlag()
  {
    return errFlag;
  }

  /**
   * @param pErrFlag pErrFlag to define
   */
  public void setErrFlag(boolean pErrFlag)
  {
    this.errFlag = pErrFlag;
  }

  /**
   * @return key
   */
  public String getKey()
  {
    return key;
  }

  /**
   * @param key key to define
   */
  public void setKey(String key)
  {
    this.key = key;
  }

  /**
   * @return technicalException
   */
  public TechnicalException getTechnicalException()
  {
    return technicalException;
  }

  /**
   * @param technicalException technicalException to define
   */
  public void setTechnicalException(TechnicalException technicalException)
  {
    this.technicalException = technicalException;
  }

  /**
   * @return businessException
   */
  public BusinessException getBusinessException()
  {
    return businessException;
  }

  /**
   * @param businessException businessException to define
   */
  public void setBusinessException(BusinessException businessException)
  {
    this.businessException = businessException;
  }


  /**
   * @return authenticationError
   */
  public String getAuthenticationError()
  {
    if(StringUtils.isBlank(authenticationError))
    {
      authenticationError = "You are disconnected from this application. "
          + "In order to log in, please, try to lunch the application from your environment.";
    }
    return authenticationError;
  }

  /**
   * @param pAuthenticationError authenticationError 
   */
  public void setAuthenticationError(String pAuthenticationError)
  {
    this.authenticationError = pAuthenticationError;
  }

  /**
   * @return authenticationTitle
   */
  public String getAuthenticationTitle()
  {
    if(StringUtils.isBlank(authenticationTitle))
    {
      authenticationTitle = "Authentication";
    }
    return authenticationTitle;
  }

  /**
   * @param pAuthenticationTitle authenticationTitle 
   */
  public void setAuthenticationTitle(String pAuthenticationTitle)
  {
    this.authenticationTitle = pAuthenticationTitle;
  }
  
}